package com.tools.scanner.ui.qrcode.scanner

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.mlkit.vision.barcode.common.Barcode
import com.tools.scanner.R
import com.tools.scanner.bean.BarcodeSchema
import com.tools.scanner.databinding.FragmentTextScannerBinding
import com.tools.scanner.utils.copyText
import com.tools.scanner.utils.shareText
import com.tools.scanner.utils.showToast

class TextFragment : BaseScannerFragment() {

    private lateinit var mBinding: FragmentTextScannerBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentTextScannerBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val content = getData(String::class.java) ?: return
        val schema = getSchema() ?: return

        mBinding.ivType.setImageResource(R.drawable.ic_barcode)
        when (schema) {
            BarcodeSchema.EAN_8 -> {
                mBinding.tvType.setText(R.string.ean_8)
            }

            BarcodeSchema.EAN_13 -> {
                mBinding.tvType.setText(R.string.ean_13)
            }

            BarcodeSchema.UPC_E -> {
                mBinding.tvType.setText(R.string.upc_e)
            }

            BarcodeSchema.UPC_A -> {
                mBinding.tvType.setText(R.string.upc_a)
            }

            BarcodeSchema.CODE_39 -> {
                mBinding.tvType.setText(R.string.code_39)
            }

            BarcodeSchema.CODE_93 -> {
                mBinding.tvType.setText(R.string.code_93)
            }

            BarcodeSchema.CODE_128 -> {
                mBinding.tvType.setText(R.string.code_128)
            }

            BarcodeSchema.ITF -> {
                mBinding.tvType.setText(R.string.itf)
            }

            BarcodeSchema.PDF_417 -> {
                mBinding.tvType.setText(R.string.PDF417)
            }

            BarcodeSchema.CODABAR -> {
                mBinding.tvType.setText(R.string.codabar)
            }

            BarcodeSchema.DATA_MATRIX -> {
                mBinding.tvType.setText(R.string.data_matrix)
            }

            BarcodeSchema.AZTEC -> {
                mBinding.tvType.setText(R.string.aztec)
            }

            else -> {
                mBinding.ivType.setImageResource(R.drawable.ic_text)
            }
        }

        mBinding.tvContent.text = content
        mBinding.tvContent.movementMethod = ScrollingMovementMethod()

        mBinding.tvCopy.setOnClickListener {
            content.copyText()
            R.string.copy_success.showToast()
        }

        mBinding.tvShare.setOnClickListener {
            content.shareText()
        }
    }

}